using System.Collections;
using System.Collections.Generic;
using UnityEngine;
/// <summary>
/// 寻路逻辑概论
/// </summary>
public class Routing : MonoBehaviour
{

    public GameObject player;
   
    //可能前进的四个点
    private List<Vector2>  canMoveList;



    public List<Vector2>  obstacleList;
    public GameObject[]  obstacles;

    public GameObject dian;

    //已经找到的路径点，
    public List<Vector2> distancePointList;

    //(0,0) ->(0,1)->(0,2)->(1,2)->(2,2)->(3,2)  先把需要走的点，依次排开，排开之后，开始动
    //这个点可能都不是连续的(消耗不消耗，行动点是随机的)
    //一定是个连续的行进过程
    //每个点有点停顿

    int i = 1;

    /// <summary>
    /// 探索路径 
    /// 递归 结束的点一定是不变的
    /// </summary>
    /// <param name="startPoint"></param>
    /// <param name="endPoint"></param>
    List<Vector2> Explore(Vector2 startPoint, Transform endPoint)
    {

        canMoveList = new List<Vector2>();
        //生成上下左右四个点的坐标，寻找四个点中，权重最短的点，作为下一个点，递归寻找
        Vector2 up = startPoint + new Vector2(0, 1);
        Vector2 down = startPoint + new Vector2(0, -1);
        Vector2 left = startPoint + new Vector2(-1, 0);
        Vector2 right = startPoint + new Vector2(1, 0);

        //检测一下四个点是否都是可行的，怎末做呢，通过tag标签，产找所有的障碍物


        canMoveList.Add(up);
        canMoveList.Add(down);
        canMoveList.Add(left);
        canMoveList.Add(right);

        //双层循环，看一下是不是存在交集，存在，就将该点移除
        /*
        for(int i=0;i<canMoveList.Count;i++){

            for(int j=0;j<obstacleList.Count;j++){
                //自动避障很简单啊
                if(canMoveList[i]==obstacleList[j]){
                    canMoveList.Remove(canMoveList[i]);
                }

            }
        }

        */

        //获取权重最小的点
        Vector2 distancePoint = findMinPoint();

        //检测，到没到终点，没到的话，继续寻路
        if (acessEnd(distancePoint))
        {
            //到达终点，路程固定，开始执行移动方法
            /*
            distancePointList.ForEach(dis =>
            {
                print("点位集合：："+dis);
            });
            */
            return distancePointList;
        } else{
            //生成虚拟的点
         //   Instantiate(dian, distancePoint, dian.transform.rotation);
            //先将之前权重最小的点，加入到可行列表
            //递归，继续寻找可行的点
            distancePointList.Add(distancePoint);
            Explore(distancePoint, player.transform);
        }
        return distancePointList;

    }


    //输入一个终点
    //开始寻路
    public List<Vector2> StartRouting(){
        //直接开始走就调这个？？？，还是说你就只是个寻路
        //开始行动
       return  Explore(transform.position,player.transform);
    }

    /// <summary>
    /// 
    /// 知道玩家 Player的坐标，坐标重新计算，计算每一个边角的权重，选择权重最小的方格前进
    /// 
    /// 
    /// </summary>

    // Start is called before the first frame update
    void Start()
    {
        player = Player.Instance.player;

        /*
        //获取障碍物列表
        obstacles = GameObject.FindGameObjectsWithTag("obstacle");
        foreach(GameObject obstacle in obstacles)
        {
            Vector2 v2=obstacle.transform.position;
            obstacleList.Add(v2);
        }


        */
    }


    //检测是否到达终点
    public bool acessEnd(Vector3 distancePoint){
            float v = Vector3.Distance(distancePoint, player.transform.position);
        
            if (v<0.1f)
            {
            return true;
            }else{
                return false;
            }
    }

    
    //寻找最短的点
    public Vector2 findMinPoint(){
            //每次计算一次对于，玩家的权重，然后知道该怎么走
            //我可以先计算出，每次三个格子的路线，。，敌人一次条约移动，巡逻模式，不能在无限制
            List<float> numList=new List<float>();
            //A*寻路算法
            Vector2 minV2=new Vector2();

            minV2=canMoveList[0];
            //无法实现自动避障

            float minDis = Vector3.Distance(canMoveList[0], player.transform.position);

             //从四个点中，分别计算权重，取最小值
            for (int i=0;i<canMoveList.Count;i++){
                    float dis = Vector3.Distance(canMoveList[i], player.transform.position);
                    if(dis<minDis){
                        //最小的权重值
                        minDis=dis;
                        //最小的V2
                        minV2=canMoveList[i];
                    }
                }
                return minV2;
        }


}




